glo dir "C:\Users\03638881\Documents\development\satellite\OrigGrid\"
cd ${dir}
glo dgp 12

forval i=1/$dgp {
	use MEBinaryDGP`i', clear
	g dgp = `i'
	drop if ame_d_4==. | ame_d_7==. 
	drop if _n>200
	if (_N!=200) {
		noi di "Too few sims in DGP `i'"
		noi su dgp nobs T beta0 theta0 theta1 
	}
	save MEBinary_s_DGP`i', replace
}

use MEBinary_s_DGP1, clear
forval i=2/$dgp {
	append using MEBinary_s_DGP`i'
}

********** COMPUTE ERROR, ABS ERROR, SQUARED ERROR ***********
*** Treats estimator 0 as the truth within each simulation *** 

loc list " "
foreach w in d x1 x2 {
	forval j = 1/7 {
		g e_ame_`w'_`j' = ame_`w'_`j' - ame_`w'_0
		g ae_ame_`w'_`j' = abs(ame_`w'_`j' - ame_`w'_0)
		g ape_ame_`w'_`j' = abs(ame_`w'_`j' - ame_`w'_0)/ame_`w'_0
		g se_ame_`w'_`j' = (ame_`w'_`j' - ame_`w'_0)^2 
		loc list "`list' med_e_ame_`w'_`j'=e_ame_`w'_`j' med_ae_ame_`w'_`j'=ae_ame_`w'_`j' med_se_ame_`w'_`j'=se_ame_`w'_`j'"
	}
}

collapse (mean) nobs T alpha beta0 theta0 theta1 fp fn e_* ae_* ape_* se_* (median) `list', by(dgp)
foreach w in d x1 x2 {
	forval j = 1/7 {
		replace se_ame_`w'_`j' = sqrt(se_ame_`w'_`j') 
		replace med_se_ame_`w'_`j' = sqrt(med_se_ame_`w'_`j') 
		
		if ("`w'" == "d") loc ww=3
		if ("`w'" == "x1") loc ww=1
		if ("`w'" == "x2") loc ww=2
		
		ren se_ame_`w'_`j' mse_ame_`j'_`ww'
		ren ae_ame_`w'_`j' mae_ame_`j'_`ww'
		ren ape_ame_`w'_`j' mape_ame_`j'_`ww'
		ren e_ame_`w'_`j' me_ame_`j'_`ww'
		ren med_se_ame_`w'_`j' med_se_ame_`j'_`ww'
		ren med_ae_ame_`w'_`j' med_ae_ame_`j'_`ww'
		ren med_e_ame_`w'_`j' med_e_ame_`j'_`ww'
		
		replace mse_ame_`j'_`ww'=100*mse_ame_`j'_`ww'
		replace me_ame_`j'_`ww'=100*me_ame_`j'_`ww'
		
		
	}
}

#delimit ;
reshape long 	mse_ame_1_ mae_ame_1_ mape_ame_1_ me_ame_1_ med_se_ame_1_ med_ae_ame_1_ med_e_ame_1_
				mse_ame_2_ mae_ame_2_ mape_ame_2_ me_ame_2_ med_se_ame_2_ med_ae_ame_2_ med_e_ame_2_
				mse_ame_3_ mae_ame_3_ mape_ame_3_ me_ame_3_ med_se_ame_3_ med_ae_ame_3_ med_e_ame_3_
				mse_ame_4_ mae_ame_4_ mape_ame_4_ me_ame_4_ med_se_ame_4_ med_ae_ame_4_ med_e_ame_4_
				mse_ame_5_ mae_ame_5_ mape_ame_5_ me_ame_5_ med_se_ame_5_ med_ae_ame_5_ med_e_ame_5_
				mse_ame_6_ mae_ame_6_ mape_ame_6_ me_ame_6_ med_se_ame_6_ med_ae_ame_6_ med_e_ame_6_
				mse_ame_7_ mae_ame_7_ mape_ame_7_ me_ame_7_ med_se_ame_7_ med_ae_ame_7_ med_e_ame_7_, 
				i(dgp nobs T alpha beta0 theta0 theta1 fp fn) j(var) ;
rensfix _ ;
sort dgp var ;
order dgp nobs T alpha beta0 theta0 theta1 fp fn var
	me_ame_1	me_ame_2	me_ame_3	me_ame_5	me_ame_6	me_ame_4	me_ame_7	
	mae_ame_1	mae_ame_2	mae_ame_3	mae_ame_5	mae_ame_6	mae_ame_4	mae_ame_7	
	mape_ame_1	mape_ame_2	mape_ame_3	mape_ame_5	mape_ame_6	mape_ame_4	mape_ame_7	
	mse_ame_1	mse_ame_2	mse_ame_3	mse_ame_5	mse_ame_6	mse_ame_4	mse_ame_7	
	med_e_ame_1	med_e_ame_2	med_e_ame_3	med_e_ame_5	med_e_ame_6	med_e_ame_4	med_e_ame_7	
	med_ae_ame_1	med_ae_ame_2	med_ae_ame_3	med_ae_ame_5	med_ae_ame_6	med_ae_ame_4	med_ae_ame_7	
	med_se_ame_1	med_se_ame_2	med_se_ame_3	med_se_ame_5	med_se_ame_6	med_se_ame_4	med_se_ame_7	;
#delimit cr

save MEBinary_formatted, replace